# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface

import sqlite3
import time
from datetime import datetime


class Spider51JobCompanyPipeline:
    def __init__(self):
        self.con = sqlite3.connect('company.db')
        self.cur = self.con.cursor()
        self.data = time.strftime('%Y-%m-%d')

        try:
            self.cur.execute('''create table Interior_Design
            (id INTEGER PRIMARY KEY AUTOINCREMENT , company text not null, company_51url, company_nature,
            company_size, company_industry, email, tel);''')
        except Exception as e:
            print(e)

    def process_item(self, item, spider):
        try:
            self.cur.execute('''
                insert into Interior_Design (company, company_51url, company_nature, 
                company_size, company_industry) values (?, ?, ?, ?, ?)
                ''', (item['company'], item['company_51url'], item['company_nature'], item['company_size'],
                      item['company_industry']))
            self.con.commit()
        except Exception as e:
            print('data save error. ', e)
            f = open('data_save_error_' + self.data + '.txt', 'a')
            f.write('\n {}----{}\n'.format(datetime.now(), e))
            f.close()
        return item

    def close_spider(self):
        self.cur.close()
        self.con.close()
